#!usr/bin/python3
# -*- coding: utf-8 -*-
# @Time     : 2019/1/18 10:27
# @Author   : Yosef-夜雨声烦
# @Email    : wurz529@foxmail.com
# @File     : session_request.py
# @Software : PyCharm
import requests
from common.mylog import Mylog

logger = Mylog("session_requests")


class SessionRequest:

    def __init__(self, url, params=None, data=None, headers=None, json=None):
        self.url = url
        self.params = params
        self.data = data
        self.headers = headers
        self.json = json

    def session_request(self, method, cookies=None):
        session = requests.session()
        session.keep_alive = False
        if method.upper() == "GET":
            try:
                res = session.get(self.url, params=self.params, headers=self.headers)
                # res = requests.get(self.url, self.params, headers=self.headers, cookies=cookies)
                logger.info("url:{}的get请求执行成功".format(self.url))
            except Exception as e:
                logger.info("执行get请求报错，报错信息是{}".format(e))
                res = e
        elif method.upper() == "POST":
            try:
                res = session.post(self.url, data=self.data, headers=self.headers, json=self.json)
                logger.info("url:{}的post请求执行成功".format(self.url))
            except Exception as e:
                logger.info("执行post请求报错，报错信息是{}".format(e))
                res = e
        else:
            logger.info("请求方式不正确，请检查请求方式是否是get或者post")
            res = "请检查请求方式！！"

        return res


if __name__ == "__main__":
    url = "http://47.107.168.87:8080/futureloan/mvc/api/member/login"
    params = {"mobilephone": "17751810779", "pwd": "123456"}
    data = {"mobilephone": "17751810779", "pwd": "123456"}

    res = SessionRequest(url,data=data).session_request("post")
    print(res.json())